package com.xhtt.modules.app.controller.cfg;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.R;
import com.xhtt.common.validator.ValidatorUtils;
import com.xhtt.common.validator.group.AddGroup;
import com.xhtt.common.validator.group.UpdateGroup;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.cfg.entity.CfgIsland;
import com.xhtt.modules.cfg.entity.CfgStand;
import com.xhtt.modules.cfg.service.CfgIslandService;
import com.xhtt.modules.cfg.service.CfgProductService;
import com.xhtt.modules.cfg.service.CfgStandService;
import com.xhtt.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 区域-岛-台
 * </p>
 *
 * @author feipc
 * @since 2019-02-18
 */
@Api(value = "数据字典-区域-岛-台", tags = "数据字典-区域-岛-台")
@RestController
@RequestMapping("app/sys/cfg/island")
public class CfgIslandController extends AbstractController {

    @Autowired
    private CfgIslandService cfgIslandService;
    @Autowired
    private CfgStandService cfgStandService;
    @Autowired
    private CfgProductService cfgProductService;

    /**
     * 获取装车区域列表
     */
    @ApiOperation("获取装车区域列表(web/app)")
    @GetMapping(value = "/loadAreaList")
    @Login
    public R loadAreaList() {
        List<CfgIsland> list = cfgIslandService.loadAreaList();
        return R.ok().put(Constant.DATA, list);
    }

    /**
     * 获取岛列表
     */
    @ApiOperation("获取岛列表(web/app)")
    @GetMapping(value = "/islandList")
    @Login
    public R islandList(@RequestParam Map<String, Object> params) {
        List<CfgIsland> list = cfgIslandService.islandList(params);
        return R.ok().put(Constant.DATA, list);
    }

    /**
     * 获取装车台列表
     */
    @ApiOperation("获取装车台列表(web/app)")
    @GetMapping(value = "/standList")
    @Login
    public R standList(@RequestParam Map<String, Object> params) {
        List<CfgStand> list = cfgStandService.standList(params);
        return R.ok().put(Constant.DATA, list);
    }

    /**
     * 获取产品列表
     */
//    @ApiOperation("获取产品列表(web/app)")
//    @GetMapping(value = "/productList")
//    @Login
//    public R productList(@RequestParam Map<String, Object> params) {
//        List<CfgProduct> list = cfgProductService.productList(params);
//        return R.ok().put(Constant.DATA, list);
//    }

    /**
     * 新增岛
     */
    @PostMapping(value = "/addIsland")
    @ApiOperation("新增岛(web)")
    @Login
    public R addIsland(@RequestBody CfgIsland cfgIsland) {
        ValidatorUtils.validateEntity(cfgIsland, AddGroup.class);
        int count = cfgIslandService.count(new LambdaQueryWrapper<CfgIsland>().eq(CfgIsland::getName, cfgIsland.getName()).eq(CfgIsland::getStatus, 1));
        if (count > 0) {
            throw new RRException("对象已存在");
        }
        cfgIslandService.save(cfgIsland);
        return R.ok();
    }

    /**
     * 新增装车台
     */
    @PostMapping(value = "/addStand")
    @ApiOperation("新增装车台(web)")
    @Login
    public R addStand(@RequestBody CfgStand cfgStand) {
        ValidatorUtils.validateEntity(cfgStand, AddGroup.class);
        int count = cfgStandService.count(new LambdaQueryWrapper<CfgStand>().eq(CfgStand::getName, cfgStand.getName()).eq(CfgStand::getStatus, 1));
        if (count > 0) {
            throw new RRException("对象已存在");
        }
        cfgStandService.save(cfgStand);
        return R.ok();
    }

    /**
     * 新增产品
     */
//    @PostMapping(value = "/addProduct")
//    @ApiOperation("新增产品(web)")
//    @Login
//    public R addProduct(@RequestBody CfgProduct cfgProduct) {
//        ValidatorUtils.validateEntity(cfgProduct, AddGroup.class);
//        int count = cfgProductService.count(new LambdaQueryWrapper<CfgProduct>().eq(CfgProduct::getId, cfgProduct.getId()).eq(CfgProduct::getStandId, cfgProduct.getStandId()));
//        if (count > 0) {
//            throw new RRException("对象已存在");
//        }
//        cfgProductService.save(cfgProduct);
//        return R.ok();
//    }

    /**
     * 删除岛
     */
    @PostMapping(value = "/deleteIsland")
    @ApiOperation("删除岛(web)")
    @Login
    public R deleteIsland(@RequestBody Integer[] ids) {
        cfgIslandService.deleteIsland(ids[0]);
        return R.ok();
    }

    /**
     * 删除装车台
     */
    @PostMapping(value = "/deleteStand")
    @ApiOperation("删除装车台(web)")
    @Login
    public R deleteStand(@RequestBody Integer[] ids) {
        cfgStandService.deleteStand(ids[0]);
        return R.ok();
    }

    /**
     * 删除产品
     */
//    @PostMapping(value = "/deleteProduct")
//    @ApiOperation("删除产品(web)")
//    @Login
//    public R deleteProduct(@RequestBody Integer[] ids) {
//        cfgProductService.removeById(ids[0]);
//        return R.ok();
//    }

    /**
     * 修改装车区域
     */
    @PostMapping(value = "/updateLoadArea")
    @ApiOperation("修改装车区域(web)")
    @Login
    public R updateLoadArea(@RequestBody CfgIsland cfgIsland) {
        Integer loadAreaType = cfgIsland.getLoadAreaType();
        String loadAreaName = cfgIsland.getLoadAreaName();
        CfgIsland island = new CfgIsland();
        island.setLoadAreaName(loadAreaName);
        cfgIslandService.update(island, new LambdaQueryWrapper<CfgIsland>().eq(CfgIsland::getLoadAreaType, loadAreaType));
        return R.ok();
    }

    /**
     * 修改岛
     */
    @PostMapping(value = "/updateIsland")
    @ApiOperation("修改岛(web)")
    @Login
    public R updateIsland(@RequestBody CfgIsland cfgIsland) {
        ValidatorUtils.validateEntity(cfgIsland, UpdateGroup.class);
        int count = cfgIslandService.count(new LambdaQueryWrapper<CfgIsland>().eq(CfgIsland::getName, cfgIsland.getName()).eq(CfgIsland::getStatus, 1).ne(CfgIsland::getId, cfgIsland.getId()));
        if (count > 0) {
            throw new RRException("对象已存在");
        }
        cfgIslandService.updateById(cfgIsland);
        return R.ok();
    }

    /**
     * 修改装车台
     */
    @PostMapping(value = "/updateStand")
    @ApiOperation("修改装车台(web)")
    @Login
    public R updateStand(@RequestBody CfgStand cfgStand) {
        ValidatorUtils.validateEntity(cfgStand, UpdateGroup.class);
        cfgStand.setIslandId(null);
        int count = cfgStandService.count(new LambdaQueryWrapper<CfgStand>().eq(CfgStand::getName, cfgStand.getName()).eq(CfgStand::getStatus, 1).ne(CfgStand::getId, cfgStand.getId()));
        if (count > 0) {
            throw new RRException("对象已存在");
        }
        cfgStandService.updateById(cfgStand);
        return R.ok();
    }

    /**
     * 修改产品
     */
//    @PostMapping(value = "/updateProduct")
//    @ApiOperation("修改产品(web)")
//    @Login
//    public R updateProduct(@RequestBody CfgProduct cfgProduct) {
//        ValidatorUtils.validateEntity(cfgProduct, UpdateGroup.class);
//        int count = cfgProductService.count(new LambdaQueryWrapper<CfgProduct>().eq(CfgProduct::getProductId, cfgProduct.getProductId()).eq(CfgProduct::getStandId, cfgProduct.getStandId()).ne(CfgProduct::getId, cfgProduct.getId()));
//        if (count > 0) {
//            throw new RRException("对象已存在");
//        }
//        cfgProductService.updateById(cfgProduct);
//        return R.ok();
//    }

    /**
     * 获取装车区域
     */
    @GetMapping(value = "/getLoadArea/{loadAreaType}")
    @ApiOperation("获取装车区域(web/app)")
    @Login
    public R getLoadArea(@PathVariable("loadAreaType") Integer loadAreaType) {
        return R.ok().put(Constant.DATA, cfgIslandService.getOne(new LambdaQueryWrapper<CfgIsland>().eq(CfgIsland::getLoadAreaType, loadAreaType).select(CfgIsland::getLoadAreaType, CfgIsland::getLoadAreaName)));
    }

    /**
     * 获取岛
     */
    @GetMapping(value = "/getIsland/{id}")
    @ApiOperation("获取岛(web/app)")
    @Login
    public R getIsland(@PathVariable("id") Integer id) {
        return R.ok().put(Constant.DATA, cfgIslandService.getById(id));
    }

    /**
     * 获取装车台
     */
    @GetMapping(value = "/getStand/{id}")
    @ApiOperation("获取装车台(web/app)")
    @Login
    public R getStand(@PathVariable("id") Integer id) {
        return R.ok().put(Constant.DATA, cfgStandService.getById(id));
    }

    /**
     * 获取产品
     */
//    @GetMapping(value = "/getProduct/{id}")
//    @ApiOperation("获取产品(web/app)")
//    @Login
//    public R getProduct(@PathVariable("id") Integer id) {
//        return R.ok().put(Constant.DATA, cfgProductService.getById(id));
//    }

}
